package com.d3works.apps.base.rest.events;

import com.d3works.app.AppSettings;
import com.d3works.apps.base.rest.http.RequestEventArgs;
import com.d3works.apps.base.rest.http.RestTemplate;
import java.util.LinkedHashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class NotifyEventServiceImpl extends RestTemplate implements INotifyEventService {

  private final Logger logger = LoggerFactory.getLogger(NotifyEventServiceImpl.class);

  public String getNotifyServiceUri() {
    return AppSettings.get().get("app.integration.outter.notification");
  }

  @Override
  protected void beforeRequest(RequestEventArgs request) throws Exception {
    // TODO Auto-generated method stub

  }

  @Override
  public void createAndSend(String type, String action, String id) {
    try {
      Map<String, Object> data = new LinkedHashMap<>();
      Map<String, Object> header = new LinkedHashMap<>();
      Map<String, Object> body = new LinkedHashMap<>();
      body.put("id", id);
      data.put("header", header);
      data.put("body", body);
      header.put("sys", "erp");
      header.put("type", type);
      header.put("action", action);

      String result = this.post(getNotifyServiceUri(), data);
      if (logger.isInfoEnabled()) {
        logger.info("调用通知接口:{}，返回：{}", getNotifyServiceUri(), result);
      }
    } catch (Exception e) {
      logger.error("发送消息失败！", e);
    }
  }
}
